package com.pan.cuiniubi.utils;

import com.pan.cuiniubi.enumn.LaminaEnum;
import org.yaml.snakeyaml.Yaml;

import java.util.HashMap;
import java.util.Map;

public class ReadYamlTool {


    private static ReadYamlTool readYamlTool;
    private Map<String,Object> map;

    private ReadYamlTool(){
        Yaml yaml = new Yaml();
        map = yaml.load(getClass().getClassLoader().getResourceAsStream("application.yml"));
        map = (Map<String, Object>) map.get("cuiniubi");
    }

    public static ReadYamlTool getInstance(){
        if (readYamlTool==null){
            readYamlTool = new ReadYamlTool();
        }
        return readYamlTool;
    }

    public Map<String,Object> getMapLamina(LaminaEnum lamina){
        Map<String,Object> resultMap =
                (Map<String, Object>) this.map.getOrDefault(lamina.name(),new HashMap<>());
        return resultMap;
    }

    public Object getMapLaminaValue(LaminaEnum lamina,String key){
        Map<String,Object> laminaMap = getMapLamina(lamina);
        Object result = laminaMap.getOrDefault(
                key,key.equals("path")?"com.pan."+lamina
                :key.equals("print")?true:null);
        if (result == null){
            throw new RuntimeException("lamina key:"+lamina+" "+key+" is not found");
        }
        return result;
    }

}
